/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Lone_Wolf
 */
public class StatsClient extends JPanel {
    
    private Fenetre fenetre;
    private Client client;
    private JScrollPane leftJSP;
    private JScrollPane rightJSP;
    private JPanel pnlTEC;
    private JPanel pnlEMP;
    private JLabel[] labelsTEC;
    private JLabel[] labelsEMP;
    private JLabel lblTEC;
    private JLabel lblEMP;
    private JButton btnRetour;
    private ArrayList<Employe> arEmp;
    private Travail_Effectue[] taches;
    private double montantTEC[];
    private ArrayList montantEMP;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    
    public StatsClient(Fenetre fenetre, Client client) {
        this.fenetre = fenetre;
        this.client = client;
        btnRetour = new JButton("Retour");
        btnRetour.addActionListener(new GererEvenements());
        taches = Travail_Effectue.values();
        montantTEC = new double[taches.length];
        arEmp = new ArrayList<Employe>();
        montantEMP = new ArrayList();
    }
    
    public void addStat(Entree e) {
        int index = indexOfTache(e.getTache());
        this.montantTEC[index] += e.getCout();
        if (!arEmp.contains(e.getEmploye())) {
            arEmp.add(e.getEmploye());
            montantEMP.add(arEmp.indexOf(e.getEmploye()), e.getCout());
        } else {
            int i = arEmp.indexOf(e.getEmploye());
            montantEMP.set(i, e.getCout() + (Double) montantEMP.get(i));
        }
    }
    
    private int indexOfTache(Travail_Effectue tache) {
        int j = -1;
        for (int i = 0; i < taches.length; i++) {
            if (taches[i].equals(tache)) {
                j = i;
                i = 1000;
            }
        }
        return j;
    }
    
    public void build() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = gbc.BOTH;
        gbc.anchor = gbc.NORTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbl.setConstraints(this, gbc);

        //add column header titles
        makeSeparator(this);
        lblEMP = new JLabel("Employés");
        lblTEC = new JLabel("Tâches en Cours");
        lblEMP.setFont(f);
        lblTEC.setFont(f);
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblTEC, gbc);
        makeBorder(this);
        this.add(lblTEC);
        makeBorder(this);
        gbc.gridwidth = gbc.REMAINDER;
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.CENTER;
        gbl.setConstraints(lblEMP, gbc);
        this.add(lblEMP);
        makeSeparator(this);

        //créer les TEC et les ajouter à gauche
        createPnlTEC();
        makeBorder(this);
        makeBorder(this);
        gbc.fill = gbc.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = gbc.FIRST_LINE_START;
        gbl.setConstraints(pnlTEC, gbc);
        leftJSP = new JScrollPane(pnlTEC);
        gbc.fill = gbc.BOTH;
        gbl.setConstraints(leftJSP, gbc);
        this.add(leftJSP);
        makeBorder(this);

        //créer les EMP et les ajouter à droite
        createPnlEMP();
        gbc.fill = gbc.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = gbc.FIRST_LINE_START;
        gbl.setConstraints(pnlEMP, gbc);
        rightJSP = new JScrollPane(pnlEMP);
        gbc.fill = gbc.BOTH;
        gbc.weightx = 1;
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(rightJSP, gbc);
        this.add(rightJSP);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder(this);

        //ajouter bouton retour
        makeSeparator(this);
        makeBorder(this);
        gbc.gridwidth = gbc.RELATIVE;
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_START;
        gbl.setConstraints(btnRetour, gbc);
        this.add(btnRetour);
        makeBorder(this);

        //finaliser tout
        this.setVisible(true);
        fenetre.setVisible(true);
    }
    
    private void createPnlTEC() {
        labelsTEC = new JLabel[taches.length];
        
        pnlTEC = new JPanel(gbl);
        gbc.fill = GridBagConstraints.BOTH;
        
        for (int i = 0; i < labelsTEC.length; i++) {
            labelsTEC[i] = new JLabel(taches[i] + " = " + montantTEC[i] + "$");
            gbc.gridwidth = gbc.RELATIVE;
            makeBorder(pnlTEC);
            gbc.weightx = 1.0;
            gbc.anchor = gbc.FIRST_LINE_START;
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.NONE;
            gbl.setConstraints(labelsTEC[i], gbc);
            pnlTEC.add(labelsTEC[i]);
        }
    }
    
    private void createPnlEMP() {
        labelsEMP = new JLabel[arEmp.size()];
        
        pnlEMP = new JPanel(gbl);
        gbc.fill = GridBagConstraints.BOTH;
        
        for (int i = 0; i < labelsEMP.length; i++) {
            labelsEMP[i] = new JLabel(arEmp.get(i).getNom() + " = " + (Double) montantEMP.get(i) + "$");
            gbc.gridwidth = gbc.RELATIVE;
            makeBorder(pnlEMP);
            gbc.weightx = 1.0;
            gbc.anchor = gbc.FIRST_LINE_START;
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.NONE;
            gbl.setConstraints(labelsEMP[i], gbc);
            pnlEMP.add(labelsEMP[i]);
        }
    }
    
    private void makeSeparator(JPanel pnl) {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        pnl.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }
    
    private void makeBorder(JPanel pnl) {
        gbc.weightx = 0.0;
        JLabel sep = new JLabel("   ");
        gbl.setConstraints(sep, gbc);
        pnl.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }
    
    class GererEvenements implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnRetour) {
                fenetre.afficherConsulterClient(client);
            }
        }
    }
}
